﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace testgame
{
    class monsterBullet
    {
        private int x;//怪兽子弹坐标x
        private int y;//怪兽子弹坐标y
        Point monbulloc;//怪物子弹位置
        private int distance;//怪兽移动速度
        static List<monsterBullet> monbullist = new List<monsterBullet>();//怪兽子弹列表
        Image monbul = Properties.Resources.monsterbullet;//怪兽子弹图片
        double k;     //斜率
        static Rectangle monbulRect; //怪兽子弹所在矩形

        //怪物子弹当前位置
        public Point Monbulloc
        {
            get { return monbulloc; }
            set { monbulloc = value; }
        }

        //给怪兽x、y、移动速度、斜率、位置赋值
        public monsterBullet(int mbx, int mby, int dis, int player_x, int player_y)
        {
            x = mbx;
            y = mby;
            distance = dis;
            k = (1.0 * (player_x - x) / (1.0 * (player_y - y)));
            monbulloc = new Point(x, y);
        }

        /// <summary>
        /// 产生敌方子弹
        /// </summary>
        public static void ProduceMonbul()
        {
            for (int i = 0; i < monster.monsters.Count; i++)
            {
                //随机产生不同速度的发射的子弹
                if (new Random().Next(10, 20) == 10)
                {
                    monsterBullet monbul = new monsterBullet(monster.monsters[i].GetLoc().X + 15, monster.monsters[i].GetLoc().Y + 30, new Random().Next(5, 10), LaunchCanister.LCx, LaunchCanister.LCy);
                    monbullist.Add(monbul);
                }
            }
        }
        /// <summary>
        /// 显示敌方子弹
        /// </summary>
        /// <param name="e"></param>
        public void ShowMonbul(Graphics e)
        {
            e.DrawImage(monbul, monbulloc);
        }

        //一个子弹移动
        public void Move()
        {
            monbulloc.Y += distance;
            monbulloc.X += (int)(distance * k);
        }
        /// <summary>
        ///一群敌方子弹不断移动
        /// </summary>
        /// <param name="e"></param>
        public static void Movemonbul(Graphics e)
        {
            for (int i = 0; i < monbullist.Count; i++)
            {
                monbullist[i].ShowMonbul(e);
                monbullist[i].Move();
                if (monbullist[i].monbulloc.Y > 840 || monbullist[i].monbulloc.X < 0 || monbullist[i].monbulloc.X > 490)
                {
                    monbullist.Remove(monbullist[i]);
                }
            }


        }
        /// <summary>
        /// 我方飞机碰撞检测方法
        /// </summary>
        public static void HitLC(Graphics g)
        {
            for (int k = 0; k < monbullist.Count; k++)
            {
                monbulRect = new Rectangle(monbullist[k].monbulloc.X, monbullist[k].monbulloc.Y, 6, 6);
                Rectangle lcRect = new Rectangle(LaunchCanister.LCx, LaunchCanister.LCy , 40, 50);
                if (monbulRect.IntersectsWith(lcRect)) //我方飞机被敌方子弹击中
                {
                    monbullist.Remove(monbullist[k]);
                    LaunchCanister.health -= 1;
                    if (LaunchCanister.score > 0)
                    {
                        LaunchCanister.score -= 1;
                    }
                }
            }
        }

    }
}
 